package com.xiguaji.tiktok.task.music;

import com.gugee.tiktok.common.model.mysql.BloggerLog;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.tiktok.task.common.job.TaskParam;
import com.xiguaji.tiktok.task.music.service.MusicService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 音乐定时任务
 */
@Service
@Slf4j
public class MusicTask {

    @Autowired
    MusicService musicService;

    @XxlJob("sendMusic2kafkaHandler")
    public ReturnT<String> sendMusic2kafkaHandler(String param) {
        long start = System.currentTimeMillis();
        try {
            log.info("开始启动音乐投递任务: params={}", param);
            XxlJobLogger.log("开始启动音乐投递任务: params={}", param);
            TaskParam jobParam = JsonUtil.jsonParse(TaskParam.class, param);
            int bloggerLogType = BloggerLog.BloggerLogType.SEND_MUSIC_2_KAFKA.getValue();
            musicService.sendMsg2Kafka(bloggerLogType,"sendMusic2kafkaHandler",jobParam);
            return ReturnT.SUCCESS;
        } catch (Exception ex) {
            XxlJobLogger.log(ex);
            return ReturnT.FAIL;
        }finally {
            log.info("投递结束  end cost total={},", (System.currentTimeMillis() - start) / 1000);
            XxlJobLogger.log("投递结束  end cost total={}, params={}", (System.currentTimeMillis() - start) / 1000,param);
        }
    }

    @XxlJob("setMusicDistributionCashToRedis")
    public ReturnT<String> setMusicDistributionCashToRedis(String param) {
        long start = System.currentTimeMillis();
        try {
            XxlJobLogger.log("开始启动标签缓存任务: params={}", param);
            musicService.setDistributionCash();
            return ReturnT.SUCCESS;
        } catch (Exception ex) {
            XxlJobLogger.log(ex);
            return ReturnT.FAIL;
        }finally {
            log.info("标签缓存结束", (System.currentTimeMillis() - start) / 1000);
            XxlJobLogger.log("标签缓存结束  end cost total={},params={}", (System.currentTimeMillis() - start) / 1000,param);
        }
    }


}
